<?php
/**
 * XML节点存储对象
 *
 * 用来存储DOM中节点数据，由此可以实现DOM持久化
 * 
 * @version $Id: IXmlNode.php 312 2008-04-23 16:29:38Z iwind $
 * @package util.xml
 */
class IXmlNode extends IObject {
	private $name;
	private $value;
	private $type;
	private $parentNode;
	private $attributeMap = array();
	private $childNodes = array();
	
	/**
	 * 构造器
	 *
	 */
	public function __construct() {
		
	}
	
	/**
	 * 添加新节点
	 *
	 * @param string $name 属性名
	 * @param string $value 属性值
	 */
	public function addAttribute($name, $value) {
		$this->attributeMap[$name] = $value;
	}
	
	/**
	 * 添加子节点
	 *
	 * @param IXmlNode $childNode 新的子节点
	 */
	public function addChildNode(IXmlNode $childNode) {
		$this->childNodes[] = $childNode;
	}
	
	/**
	 * 取得属性值
	 *
	 * @param string $name 属性名
	 * @return string/null
	 */
	public function getAttribute($name) {
		if (isset($this->attributeMap[$name])) {
			return $this->attributeMap[$name];
		}
		return null;
	}
	
	/**
	 * 取得所有属性
	 *
	 * @return array
	 */
	public function getAttributeMap() {
		return $this->attributeMap;
	}
	
	/**
	 * 使用名字取得对应的节点
	 *
	 * @param string $childName 子节点名
	 * @param boolean $caseSensitive 是否大小写敏感
	 * @return array
	 */
	public function getNamedChildNodes($childName, $caseSensitive = true) {
		$childNodes = array();
		foreach ($this->childNodes as $node) {
			$nodeName = $node->getName();
			if (($caseSensitive && $nodeName === $childName) || (!$caseSensitive && strcasecmp($nodeName, $childName) == 0)) {
				$childNodes[] = $node;
			}
		}
		return $childNodes;
	}
	
	/**
	 * 取得所有子节点
	 *
	 * @return array
	 */
	public function getChildNodes() {
		return $this->childNodes;
	}
	
	/**
	 * 取得节点名
	 *
	 * @return string
	 */
	public function getName() {
		return $this->name;
	}
	
	/**
	 * 取得父节点
	 *
	 * @return IXmlNode
	 */
	public function getParentNode() {
		return $this->parentNode;
	}
	
	/**
	 * 取得节点类型
	 *
	 * @return string
	 */
	public function getType() {
		return $this->type;
	}
	
	/**
	 * 取得节点值
	 *
	 * @return string
	 */
	public function getValue() {
		return $this->value;
	}
	
	/**
	 * 设置节点名
	 *
	 * @param string $name 节点名
	 */
	public function setName($name) {
		$this->name = $name;
	}
	
	/**
	 * 设置节点类型
	 *
	 * @param string $type 节点类型
	 */
	public function setType($type) {
		$this->type = $type;
	}
	
	/**
	 * 节点值
	 *
	 * @param string $value 节点值
	 */
	public function setValue($value) {
		$this->value = $value;
	}
	
	/**
	 * 设置父节点
	 *
	 * @param IXmlNode $parentNode 父节点
	 */
	public function setParentNode(IXmlNode $parentNode) {
		$this->parentNode = $parentNode;
	}
	
	/**
	 * 从DOM节点中加载IXmlNode节点树
	 *
	 * @param DOMNode $domNode DOM节点
	 */
	public function loadFromDOM(DOMNode $domNode) {
		$this->setType($domNode->nodeType);
		$this->setName($domNode->nodeName);
		$this->setValue($domNode->nodeValue);
		if ($domNode->hasAttributes()) {
			$attributes = $domNode->attributes;
			foreach ($attributes as $attribute) {
				$this->addAttribute($attribute->nodeName, $attribute->nodeValue);
			}
		}
		if ($domNode->hasChildNodes()) {
			$childNodes = $domNode->childNodes;
			foreach ($childNodes as $childNode) {
				$xmlNode = new IXmlNode();
				$xmlNode->loadFromDOM($childNode);
				$xmlNode->setParentNode($this);
				$this->addChildNode($xmlNode);
			}
		}
	}
	
	/**
	 * 从XML字符串中加载节点树
	 *
	 * @param string $xmlString XML字符串
	 * @since 0.0.3
	 */
	public function loadFromString($xmlString) {
		$dom = new DOMDocument();
		$dom->loadXML($xmlString);
		$this->loadFromDOM($dom);
	}
}

?>